"""
@Project:study_platform
@File:serializer.py
@Author:韩晓雷
@Date:20:31
"""


import re
from rest_framework import serializers
from user.models import *
from rest_framework.validators import ValidationError


# 学校序列化器
class SchoolSer(serializers.ModelSerializer):

    class Meta:
        model = SchoolModel
        fields = "__all__"

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}学校名称由2到9为汉字组成')

        return attrs

